package com.gongpingjialoan.activity.sell;

import android.animation.ValueAnimator;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ClipDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.car.SellCarAddressActivity;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.bean.car.SelectShopSucceedData;
import com.gongpingjialoan.imageload.ImageLoad;

import java.util.Arrays;
import java.util.List;

public class SellSucceedActivity extends BaseActivity {

    private LinearLayout shopsourcelayout;
    private ImageView carlogoI;
    private List<String> succeedNameList;
    private SelectShopSucceedData data;
    private LayoutInflater inflater;
    private ClipDrawable clipDrawable;
    private String car_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true;
        action = "sell_pushDealers";
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sell_succeed);
        initView();
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Bundle bd = msg.getData();
            int index = bd.getInt("index");
            ImageLoad.LoadImage(carlogoI, data.shopSucceed.get(index).getShopUrl(), R.drawable.trans, R.drawable.trans,SellSucceedActivity.this);
            LinearLayout layout = (LinearLayout) shopsourcelayout.getChildAt(index);
            ImageView img = (ImageView) layout.findViewById(R.id.check);
            TextView result = (TextView) layout.findViewById(R.id.result);
            TextView carname = (TextView) layout.findViewById(R.id.shopname);
            View footline = layout.findViewById(R.id.footline);
            switch (msg.what) {
                case 1:
                    carname.setTextColor(Color.parseColor("#333333"));
                    img.setVisibility(View.VISIBLE);
                    img.setImageResource(R.drawable.sell_circle);
                    result.setVisibility(View.VISIBLE);
                    result.setText("正在预约");

                    break;
                case 2:
                    String name = data.shopSucceed.get(index).getShopName();
                    if (succeedNameList.contains(name)) {
                        img.setImageResource(R.drawable.checked);
                        result.setText("预约成功");
                    } else {
                        img.setImageResource(R.drawable.cha);
                        result.setText("预约失败");
                    }
                    setAnimition(footline, 0);
                    break;
            }
        }
    };

    private void initView() {
        setTitle("快速卖车");

        data = SelectShopSucceedData.getInstance();
        String[] shopName = getIntent().getStringArrayExtra("shopName");
        car_id = getIntent().getStringExtra("car_id");
        succeedNameList = Arrays.asList(shopName);

        shopsourcelayout = (LinearLayout) findViewById(R.id.shopsourcelayout);
        carlogoI = (ImageView) findViewById(R.id.carlogo);

        inflater = LayoutInflater.from(this);
        int datasize = data.shopSucceed.size();
        for (int i = 0; i < datasize; i++) {

            View view = inflater.inflate(R.layout.item_sell_succeed, null);
            ImageView img = (ImageView) view.findViewById(R.id.check);
            TextView carname = (TextView) view.findViewById(R.id.shopname);
            TextView result = (TextView) view.findViewById(R.id.result);
            View footline = view.findViewById(R.id.footline);
            if (i == 0) {
                result.setText("正在预约");
                img.setImageResource(R.drawable.sell_circle);
                carname.setTextColor(Color.parseColor("#333333"));
            } else {
                result.setVisibility(View.INVISIBLE);
                img.setVisibility(View.INVISIBLE);
                carname.setTextColor(Color.parseColor("#cccccc"));
            }
            if (i == data.shopSucceed.size() - 1) {
                footline.setVisibility(View.GONE);
                footline.setTag("false");
            } else {
                footline.setTag("true");
            }
            String name = data.shopSucceed.get(i).getShopName();
            carname.setText(name);
            shopsourcelayout.addView(view);
            sendMsg(i);
        }
    }


    private void setAnimition(final View view, int duration) {
        if (!"true".equals(view.getTag().toString())) {
            finish();
            //startActivity(new Intent(this, SellCarStatusActivity.class));
            Intent intent = new Intent(this, SellCarAddressActivity.class);
            intent.putExtra("car_id", car_id);
            startActivity(intent);
        } else {
            clipDrawable = (ClipDrawable) view.getBackground();
            view.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ValueAnimator value = ValueAnimator.ofInt(0, 10000);
                    value.setDuration(1500);
                    value.setInterpolator(new AccelerateDecelerateInterpolator());
                    value.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                        @Override
                        public void onAnimationUpdate(ValueAnimator animation) {
                            int value = (int) animation.getAnimatedValue();
                            clipDrawable.setLevel(value);
                        }
                    });
                    value.start();
                }
            }, duration);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
    }

    private void sendMsg(int index) {
        Message msg = new Message();
        msg.what = 1;
        Bundle bd = new Bundle();
        bd.putInt("index", index);
        msg.setData(bd);
        mHandler.sendMessageDelayed(msg, index * 2000);

        Message msg2 = new Message();
        msg2.what = 2;
        Bundle bd2 = new Bundle();
        bd2.putInt("index", index);
        msg2.setData(bd2);
        mHandler.sendMessageDelayed(msg2, 2000 + index * 2000);
    }

}
